package com.qimu.saiyou.service.impl;

import cn.hutool.core.collection.CollUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.qimu.saiyou.common.ErrorCode;
import com.qimu.saiyou.constant.CommonConstant;
import com.qimu.saiyou.exception.BusinessException;
import com.qimu.saiyou.exception.ThrowUtils;
import com.qimu.saiyou.mapper.QuestionBankMapper;
import com.qimu.saiyou.model.dto.questionbank.QuestionBankQueryRequest;
import com.qimu.saiyou.model.entity.Question;
import com.qimu.saiyou.model.entity.QuestionBank;
import com.qimu.saiyou.model.entity.QuestionBankRef;
import com.qimu.saiyou.model.vo.QuestionBankVo;
import com.qimu.saiyou.model.vo.QuestionVO;
import com.qimu.saiyou.service.QuestionBankRefService;
import com.qimu.saiyou.service.QuestionBankService;
import com.qimu.saiyou.service.QuestionService;
import com.qimu.saiyou.utils.SqlUtils;
import org.apache.commons.lang3.ObjectUtils;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.util.List;
import java.util.stream.Collectors;

/**
 * @author: FeiXiang
 * @description 针对表【question_bank(题库)】的数据库操作Service实现
 * @createDate 2024-02-28 20:36:14
 */
@Service
public class QuestionBankServiceImpl extends ServiceImpl<QuestionBankMapper, QuestionBank>
        implements QuestionBankService {
    @Resource
    private QuestionService questionService;
    @Resource
    private QuestionBankRefService questionBankRefService;

    @Override
    public void validQuestionBank(QuestionBank questionBank, boolean add) {
        if (questionBank == null) {
            throw new BusinessException(ErrorCode.PARAMS_ERROR);
        }
        Long id = questionBank.getId();
        String title = questionBank.getTitle();
        String remark = questionBank.getRemark();


        // 创建时，参数不能为空
        if (add) {
            ThrowUtils.throwIf(StringUtils.isAnyBlank(title), ErrorCode.PARAMS_ERROR);
        }
        // 有参数则校验
        if (StringUtils.isNotBlank(title) && title.length() > 1000) {
            throw new BusinessException(ErrorCode.PARAMS_ERROR, "题库名称过长");
        }
    }

    @Override
    public QueryWrapper<QuestionBank> getQueryWrapper(QuestionBankQueryRequest questionBankQueryRequest) {
        QueryWrapper<QuestionBank> queryWrapper = new QueryWrapper<>();
        if (questionBankQueryRequest == null) {
            return queryWrapper;
        }
        String searchText = questionBankQueryRequest.getSearchText();
        String sortField = questionBankQueryRequest.getSortField();
        String sortOrder = questionBankQueryRequest.getSortOrder();
        Long id = questionBankQueryRequest.getId();

        // 拼接查询条件
        if (StringUtils.isNotBlank(searchText)) {
            queryWrapper.and(qw -> qw.lambda().like(QuestionBank::getRemark, searchText).or().like(QuestionBank::getTitle, searchText));
        }
        queryWrapper.eq(ObjectUtils.isNotEmpty(id), "id", id);
        queryWrapper.orderBy(SqlUtils.validSortField(sortField), sortOrder.equals(CommonConstant.SORT_ORDER_ASC),
                sortField);
        return queryWrapper;
    }

    @Override
    public Page<QuestionBankVo> getQuestionBankVoPage(Page<QuestionBank> questionBankPage, HttpServletRequest request) {
        List<QuestionBank> questionList = questionBankPage.getRecords();
        Page<QuestionBankVo> questionBankVoPage = new Page<>(questionBankPage.getCurrent(), questionBankPage.getSize(), questionBankPage.getTotal());
        if (CollUtil.isEmpty(questionList)) {
            return questionBankVoPage;
        }
        List<QuestionBankVo> questionBankVoList = questionList.stream().map(this::getQuestionBankVo).collect(Collectors.toList());
        questionBankVoPage.setRecords(questionBankVoList);
        return questionBankVoPage;
    }

    @Override
    public QuestionBankVo getQuestionBankVo(QuestionBank questionBank, HttpServletRequest request) {
        return getQuestionBankVo(questionBank);
    }

    public QuestionBankVo getQuestionBankVo(QuestionBank questionBank) {
        QuestionBankVo questionBankVo = QuestionBankVo.objToVo(questionBank);
        QueryWrapper<QuestionBankRef> queryWrapper = new QueryWrapper<>();
        queryWrapper.lambda().eq(QuestionBankRef::getQuestionBankId, questionBank.getId());
        List<Long> questionIdList = questionBankRefService.list(queryWrapper).stream().map(QuestionBankRef::getQuestionId).collect(Collectors.toList());
        List<Question> questions = questionService.listByIds(questionIdList);
        Page<Question> questionPage = new Page<>();
        questionPage.setRecords(questions);
        Page<QuestionVO> questionVOPage = questionService.getQuestionVOPage(questionPage);
        questionBankVo.setQuestionList(questionVOPage.getRecords());
        return questionBankVo;
    }
}




